package com.hw.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.constants.ConstantCode;
import com.hw.dao.CategoriesDao;
import com.hw.exceptions.MyBaseException;
import com.hw.pojo.Categories;
import com.hw.service.CategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @ClassName: CategoriesServiceImpl
 * @Description:
 * @Author: vie0134
 * @Date: 2020-12-15 16:45
 * @Version: 1.0
 **/
@Service
public class CategoriesServiceImpl implements CategoriesService {

    @Autowired
    private CategoriesDao categoriesDao;

    @Override
    public PageInfo<Categories> findAllCategories(int pageNum, int pageSize, String categoryname, String status) {
        PageHelper.startPage(pageNum, pageSize);

        try {
            List<Categories> allCategories = categoriesDao.findAllCategories(categoryname, status);
//            allCategories.forEach(categories -> {categories.commandsjson(null);});
            return new PageInfo<>(allCategories);
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new MyBaseException("查询异常", ConstantCode.ERROR);
    }

    @Override
    public void addCategories(Categories categories) {
        if (categories == null || StringUtils.isEmpty(categories)) {
            throw new MyBaseException("数据为空", ConstantCode.NULL_DATA);
        }
        try {
            categoriesDao.addCategories(categories);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException(e.getMessage(), ConstantCode.ERROR);
        }
    }

    @Override
    public void updateCategories(Categories categories) {
        if (categories == null || categories.getId() == null || (StringUtils.isEmpty(categories.getCommandsjson()) && StringUtils.isEmpty(categories.getCategoryname()))) {
            throw new MyBaseException("分类信息不完整", ConstantCode.DATA_NOT_TRANSFER);
        }
        try {
            categoriesDao.updateCategories(categories);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException(e.getMessage(), ConstantCode.DATA_NOT_TRANSFER);
        }
    }

    @Override
    public Categories findCategoriesById(Integer id) {
        if (id == null) {
            throw new MyBaseException("id不能为空", ConstantCode.DATA_NOT_TRANSFER);
        }
        try {
            Categories categories = categoriesDao.findCategoriesById(id);
            if (categories == null) {
                throw new MyBaseException("没有数据", ConstantCode.NULL_DATA);
            }
            return categories;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException(e.getMessage(), ConstantCode.NULL_DATA);
        }
    }

    @Override
    public void deleteCategoriesById(Integer id) {
        if (id == null) {
            throw new MyBaseException("没有传递id", ConstantCode.DATA_NOT_TRANSFER);
        }
        try {
            categoriesDao.deleteCategoriesById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException("查询异常", ConstantCode.ERROR);
        }
    }

    @Override
    public void deleteCategoriesByIds(List<String> ids) {
        if (ids == null || StringUtils.isEmpty(ids)) {
            throw new MyBaseException("没有传递id", ConstantCode.DATA_NOT_TRANSFER);
        }
        try {
            categoriesDao.deleteCategoriesByIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException("查询异常", ConstantCode.ERROR);
        }
    }
}
